package handlers

import (
	"bytes"
	"errors"
	"github.com/nfnt/resize"
	"image"
	"image/draw"
	"image/jpeg"
)

type WriteBuffer struct {
	B      []byte
	X      int
	Y      int
	Width  int
	Height int
}

func (w *WriteBuffer) Process(img *image.RGBA) (*image.RGBA, error) {
	copyImg, err := jpeg.Decode(bytes.NewReader(w.B))
	if err != nil {
		return img, errors.New("读取微信code失败. " + err.Error())
	}
	if w.Width > 0 || w.Height > 0 {
		copyImg = resize.Resize(uint(w.Width), uint(w.Height), copyImg, resize.Lanczos3)
	}

	draw.Draw(img, img.Bounds().Add(image.Pt(w.X, w.Y)), copyImg, copyImg.Bounds().Min, draw.Src)
	return img, nil
}
